package org.remixlab.gnutopic.pojo;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringEscapeUtils;
import org.remixlab.gnutopic.client.CircleButton;

/**
 * Clase que representa un mensaje publicado por un usuario en el foro.
 * @author avelandia
 *
 */

public class ForumMessage {
	
	private String threadId;
	
	private String title;	//el titulo del mensaje.
	
	private String text;	//El texto que tiene el mensaje
	private Date date;		//fecha en que se public� el mensaje
	
	private String userName; //el nombre del usuario que publicó el mensaje
	
	private int id = -1;	//indica el número del mensaje.
	
	private int messageId = -1;	//indica el número del mensaje.
	
	private CircleButton circle; //representa un mensaje en el hilo
	
	public String getThreadId() {
		return threadId;
	}

	public void setThreadId(String threadId) {
		this.threadId = threadId;
	}

	public int getMessageId() {
		return messageId;
	}

	public void setMessageId(int messageId) {
		this.messageId = messageId;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getText() {
		return text;
	}
	
	public void setText(String text) {
		this.text = text;
	}
	
	public Date getDate() {
		return date;
	}
	
	public void setDate(Date date) {
		this.date = date;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public CircleButton getCircle() {
		return circle;
	}

	public void setCircle(CircleButton circle) {
		this.circle = circle;
	}
	
	public String toDocXML (){
		String result = "";
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("\t<doc>\n");
		
		buffer.append("\t\t<field name=\"id\">");
		buffer.append(this.threadId+"."+this.messageId);
		buffer.append("</field>\n");
		
		buffer.append("\t\t<field name=\"username\">");
		buffer.append(this.userName);
		buffer.append("</field>\n");
		
		buffer.append("\t\t<field name=\"date\">");
		buffer.append(StringEscapeUtils.escapeJava(this.date.toString()));
		buffer.append("</field>\n");
		
		buffer.append("\t\t<field name=\"message\">");
		buffer.append(StringEscapeUtils.escapeXml(this.text));
		buffer.append("</field>\n");
		
		buffer.append("\t</doc>\n");
		
		result = buffer.toString();
		
		return result;
	}
	
	public String toString(){
		String result = "";
		
		StringBuffer buffer = new StringBuffer();
		
		buffer.append("User: ");
		buffer.append(userName);
		
		buffer.append("\nDate: ");
		buffer.append(date);
		
		buffer.append("\n\nMessage: \n");
		buffer.append(text);
		
		result = buffer.toString();
		
		return result;
	}

	public String toXML() {
		String result = "";
		
		StringBuffer buffer = new StringBuffer();
		
		DateFormat formatter = new SimpleDateFormat("MM-dd-yy 'at' HH:mm:ss");
		
		buffer.append("\t\t\t\t\t<message>\n");
		
		buffer.append("\t\t\t\t\t\t<messageId>");
		buffer.append(this.messageId);
		buffer.append("</messageId>\n");
		
		buffer.append("\t\t\t\t\t\t<messageUser>");
		buffer.append(this.userName);
		buffer.append("</messageUser>\n");
		
		buffer.append("\t\t\t\t\t\t<messageDate>");
		buffer.append(formatter.format(this.date));
		buffer.append("</messageDate>\n");
		
		buffer.append("\t\t\t\t\t\t<messageText>");
		buffer.append(StringEscapeUtils.escapeXml(this.text));
		buffer.append("</messageText>\n");
		
		buffer.append("\t\t\t\t\t</message>\n");
		
		result = buffer.toString();
		
		return result;
	}
	
}
